<?php

/* CAT:Line chart */

/* pChart library inclusions */
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");



/* Create and populate the pData object */
$MyData = new pData();
include "../database.php";
/* Create and populate the pData object */
$hoursRemaining = burnDown("hours_remaining");
//Work remaining on specific date
$workToDo = array();
//Workload, "new project time estimate"
$workload = burnDown("hours_spent");
//All dates
$date = array();
//Counting dates
$count = array();
//Ideal workrate
$ideal = array();
//Get all start sprint dates
$sprints = getSprints();
//GET first value of remaining time
$values = array_values($hoursRemaining);
$firstValue = array_shift($values);
$lastValue = end($values);
//Decreasing idealy if we would work "perfectly"
$koeficient = ($firstValue - $lastValue) / count($hoursRemaining);

$i = 1;

foreach ($hoursRemaining as $key => $val) {
    $ideal[] = ($firstValue - $koeficient * $i) + $lastValue;
    $workToDo[] = $val;
    $date[] = $key;
    $count[] = $i;
    $workload[$i-1]+=$val;
    $i++;
}
//Indexes and titles for graph lables
$sprint_indexes = array();
$sprint_titles = array();

foreach ($sprints as $key => $val) {
    $ind = array_search($val, $date);
    //If that date does not exist
    if ($val &&($ind ==0 || $ind != NULL)) {
        $sprint_indexes[] = $ind;
        $sprint_titles[] = "Sprint ".count($sprint_indexes);
    }
}

$MyData->addPoints($workToDo, "Work remaining");
$MyData->addPoints($workload, "Workload");
//$MyData->addPoints($ideal,"Ideal workrate"); 
$MyData->setAxisName(0, "Hours of work");

$MyData->addPoints($count, "Days");
$MyData->setSerieDescription("Days", "Date");
$MyData->setAbscissa("Days");
$MyData->setAbscissaName("Days from start of first sprint");


/* Create the pChart object */
$myPicture = new pImage(850, 400, $MyData);

/* Turn of Antialiasing */
$myPicture->Antialias = FALSE;

/* Add a border to the picture */
$myPicture->drawRectangle(0, 0, 849, 399, array("R" => 0, "G" => 0, "B" => 0));

/* Write the chart title */
$myPicture->setFontProperties(array("FontName" => "fonts/Forgotte.ttf", "FontSize" => 12));
//$myPicture->drawText(150, 35, "Burndown chart", array("FontSize" => 20, "Align" => TEXT_ALIGN_BOTTOMMIDDLE));

/* Set the default font */
$myPicture->setFontProperties(array("FontName" => "fonts/pf_arma_five.ttf", "FontSize" => 9));

/* Define the chart area */
$myPicture->setGraphArea(60, 40, 800, 370);
/* Draw the scale */
$scaleSettings = array("XMargin" => 10, "YMargin" => 10, "Floating" => TRUE, "GridR" => 200, "GridG" => 200, "GridB" => 200, "DrawSubTicks" => TRUE, "CycleBackground" => TRUE);
$myPicture->drawScale($scaleSettings);

/* Turn on Antialiasing */
$myPicture->Antialias = TRUE;

/* Draw the line chart */
$myPicture->drawLineChart();
$myPicture->drawPlotChart(array("DisplayValues" => FALSE, "PlotBorder" => TRUE, "BorderSize" => 2, "Surrounding" => -60, "BorderAlpha" => 80));

/* Write the chart legend */
$myPicture->drawLegend(650, 20, array("Style" => LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL));

//RENDER labels
$LabelSettings = $LabelSettings = array("TitleR"=>255,"TitleG"=>255,"TitleB"=>255, "DrawSerieColor"=>FALSE,"TitleMode"=>LABEL_TITLE_BACKGROUND, "OverrideTitle"=>"Start","ForceLabels"=>$sprint_titles, "GradientEndR"=>220,"GradientEndG"=>205,"GradientEndB"=>220, "TitleBackgroundG"=>160);
$myPicture->writeLabel("Workload", $sprint_indexes,$LabelSettings);
//$myPicture->writeLabel("Probe 1",1);
//$LabelSettings = $LabelSettings = array("TitleR"=>255,"TitleG"=>255,"TitleB"=>255, "DrawSerieColor"=>FALSE,"TitleMode"=>LABEL_TITLE_BACKGROUND, "OverrideTitle"=>"Sprint 1","ForceLabels"=>"Start", "GradientEndR"=>220,"GradientEndG"=>255,"GradientEndB"=>220, "TitleBackgroundG"=>155);

//$myPicture->writeLabel("Sprint 1 start", 0, $LabelSettings);
/* Render the picture (choose the best way) */
$myPicture->autoOutput("pictures/example.drawLineChart.simple.png");
?>
